<?php
/**
 * @version $Id: unocloud.php 2 2010-05-25 10:13:33Z vladzimirs $
 * @package UnoCloud
 * @copyright Авторские права (C) 2009-2010 UnoCloud team & (C) 2007-2010 Joostina team &. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * UnoCart! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл help/copyright.php.
 */

defined('_VALID_MOS') or die('Restricted access');

global $mosConfig_absolute_path, $mosConfig_lang;

require_once ($mainframe->getPath('front_html'));
require_once ($mosConfig_absolute_path .
    "/administrator/components/com_unocloud/includes/config.class.php");
require_once ($mosConfig_absolute_path .
    "/administrator/components/com_unocloud/includes/tag.creator.class.php");

if (!@include_once ($mosConfig_absolute_path .
    "/administrator/components/com_unocloud/language/" . $mosConfig_lang . ".php")) {
    include_once ($mosConfig_absolute_path .
        "/administrator/components/com_unocloud/language/russian.php");
}

function updateWords($debug = 0)
{
    global $database, $mosConfig_dbprefix, $mosConfig_mailfrom, $mosConfig_fromname,
        $mosConfig_cachepath, $mosConfig_absolute_path;

    $UNOCLOUD_config = new UnoCloud_config();

    if (($debug == 1) && ($UNOCLOUD_config->getValue("UNOCLOUD_debug") ==
        mosGetParam($_REQUEST, 'hash'))) {
        $debug = 1;
        echo ("Start UnoCloud debug!<br>");
    } else {
        $debug = 0;
    }

    $logfile = $mosConfig_absolute_path .
        "/administrator/components/com_unocloud/runlog.php";
    //$lockfile=$mosConfig_cachepath."/UNOCLOUD_lockfile_";


    if (file_exists($logfile)) {

        if ($debug) {
            $filetime = filemtime($logfile);
            echo ("File time is " . date("d-m-Y  G:i", $filetime) . "<br>");
            if (!($filetime < (time() - 10))) //security - 10 sec delay
                {
                echo ("Wait 10s and refresh this page for debug report.");
                return false;
            }
        }
        require ($logfile);
    } else {
        if (!touch($logfile)) {
            if ($debug)
                echo ("Error creating $logfile.<br>");

        } else {
            if ($debug)
                echo ("Creating $logfile.<br>");
        }
        return false;
    }


    $now = time();
    $run = $UNOCLOUD_nextrun;

    if (empty($run))
        $run = $now;


    if ((($run <= $now) && ($starttime <= $now)) || ($debug)) {


        if ($rows = $UNOCLOUD_config->getValues('scheduler')) {
            foreach ($rows as $row) {
                if (!empty($row)) {
                    $val = "\$" . $row->UNOCLOUD_key . " = \$row->UNOCLOUD_value;";
                    eval($val);
                }
            }
        }

        $starttime = mktime($UNOCLOUD_run_hour, $UNOCLOUD_run_minute, 0, $UNOCLOUD_run_month,
            $UNOCLOUD_run_day, $UNOCLOUD_run_year);
        if ($UNOCLOUD_run_period_unit == 1) //hours
            {
            $period = $UNOCLOUD_run_period * 3600;
            $nextrun = $now + $period;
        }

        if ($UNOCLOUD_run_period_unit == 2) //days
            {
            $nextrun = mktime($UNOCLOUD_run_hour, $UNOCLOUD_run_minute, 0, date("m"), date("d") +
                $UNOCLOUD_run_period, date("Y"));
        }

        @chmod($logfile, 0666);
        if (is_writable($logfile)) {
            if ($fp = fopen("$logfile", "w")) {
                if (!flock($fp, LOCK_EX)) {
                    if ($debug)
                        echo "Couldn't lock $logfile !<br>";
                    return false;
                }
                if (empty($nextrun))
                    $nextrun = 0;
                if (empty($now))
                    $now = time();
                $config = "<?php\n";
                $config .= '$UNOCLOUD_nextrun = ' . $nextrun . ";\n";
                $config .= '$UNOCLOUD_runlog = ' . $now . ";\n";
                $config .= "?>";

                if (fputs($fp, $config, strlen($config))) {
                    if ($debug)
                        echo "Ok writing $logfile !<br>";
                } else {
                    if ($debug)
                        echo "Error writing $logfile !<br>";
                    return false;
                }
            } else {
                if ($debug)
                    echo ("Error fopen $logfile !<br>");
                return false;
            }
        } else {
            if ($debug)
                echo ("File is not writeable !<br>");
            return false;
        }

        $time_start = getmicrotime();

        //echo("Run createCloudArray() !<br>");

        $tagCreator = new UnoCloud_TagCreator($UNOCLOUD_config);
        $tagCreator->debug = $debug;
        $tagCreator->createCloudArray();
        $tagCreator->eraseZeroCounter();
        $time_end = getmicrotime();
        $time = $time_end - $time_start;
        $time = round($time, 6);
        $time = UNOCLOUD_GENTIME . " : " . $time . " s";
        if (empty($UNOCLOUD_run_limit))
            $UNOCLOUD_run_limit = 100;
        if ($debug)
            echo ($time . "<br>" . UNOCLOUD_LAST_RUN . " : " . date("d-m-Y  G:i", $now) .
                "<br>" . UNOCLOUD_NEXT_RUN . " : " . date("d-m-Y  G:i", $nextrun) . "<br>");
        $database->setQuery("SELECT word,counter FROM " . $mosConfig_dbprefix .
            "unocloud_stat WHERE new=1 order by counter DESC LIMIT " . $UNOCLOUD_run_limit);
        if ($rows = $database->loadObjectList()) {
            $i = 0;
            foreach ($rows as $row) {
                $i++;
                $words .= $row->word . " ( " . $row->counter . " )\n";
            }
            if ($debug)
                echo ("New words were added to the database.<br>");
            $unocloud_footer = str_replace("%1", $UNOCLOUD_run_limit, UNOCLOUD_FOOTER_EMAIL);
            $unocloud_footer = str_replace("%2", $i, $unocloud_footer);
            $message = UNOCLOUD_BODY_EMAIL . "\n\n" . $words . "\n" . $unocloud_footer . "\n" .
                $time . "\n" . UNOCLOUD_LAST_RUN . " : " . date("d-m-Y  G:i", $now) . "\n" .
                UNOCLOUD_NEXT_RUN . " : " . date("d-m-Y  G:i", $nextrun);
            if (!empty($UNOCLOUD_admin_email)) {
                if (mosMail($mosConfig_mailfrom, $mosConfig_fromname, $UNOCLOUD_admin_email,
                    UNOCLOUD_TITLE_EMAIL, $message)) {
                    if ($debug)
                        echo ("E-mail sent to $UNOCLOUD_admin_email !<br>");
                } else {
                    if ($debug)
                        echo ("Error sending e-mail !<br>");
                }
            }
        } else
            if ($debug)
                echo ("No new words are added to the database.<br>");

        flock($fp, LOCK_UN);
        fclose($fp);
    }
    if ($debug)
        echo ("Stop UnoCloud debug!<br>");
    return true;
}

function getmicrotime()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}


function UNOCLOUD_search($search)
{
    global $database, $mosConfig_absolute_path, $mosConfig_list_limit, $_MAMBOTS, $mainframe;

    $search = $database->getEscaped(trim($search));
    $runSearch = true;
    $searchword = $search;
    echo ("Search!<br>");


    if ($Itemid > 0 && $Itemid != 99999999) {
        $menu = $mainframe->get('menu');
        $params = new mosParameters($menu->params);
        $params->def('page_title', 1);
        $params->def('pageclass_sfx', '');
        $params->def('header', $menu->name, _SEARCH_TITLE);
        $params->def('back_button', $mainframe->getCfg('back_button'));
    } else {
        $params = new mosParameters('');
        $params->def('page_title', 1);
        $params->def('pageclass_sfx', '');
        $params->def('header', _SEARCH_TITLE);
        $params->def('back_button', $mainframe->getCfg('back_button'));
    }

    $query = "SELECT plugin FROM #__unocloud_plugins WHERE published=1";
    $database->setQuery($query);
    if ($plugs = $database->loadObjectList()) {
        foreach ($plugs as $plug) {
            echo ($plug->plugin . " " . $search . "<br>");
            if (is_file($mosConfig_absolute_path .
                "/administrator/components/com_unocloud/plugins/" . $plug->plugin)) {
                include ($mosConfig_absolute_path .
                    "/administrator/components/com_unocloud/plugins/" . $plug->plugin);
            }

            echo ($query);
            //var_dump($results);

            $totalRows = 0;

            $rows = array();
            for ($i = 0, $n = count($results); $i < $n; $i++) {
                $rows = array_merge((array )$rows, (array )$results[$i]);
            }

            $totalRows = count($rows);

            for ($i = 0; $i < $totalRows; $i++) {
                $text = &$rows[$i]->text;

                if ($searchphrase == 'exact') {
                    $searchwords = array($searchword);
                    $needle = $searchword;
                } else {
                    $searchwords = explode(' ', $searchword);
                    $needle = $searchwords[0];
                }

                $text = mosPrepareSearchContent($text, 200, $needle);

                foreach ($searchwords as $hlword) {
                    $text = preg_replace('/' . preg_quote($hlword, '/') . '/i',
                        '<span class="highlight">\0</span>', $text);
                }

                if (strpos($rows[$i]->href, 'http') == false) {
                    $url = parse_url($rows[$i]->href);
                    parse_str(@$url['query'], $link);

                    // determines Itemid for Content items where itemid has not been included
                    if (isset($rows[$i]->type) && @$link['task'] == 'view' && isset($link['id']) &&
                        !isset($link['Itemid'])) {
                        $itemid = '';
                        $_itemid = $mainframe->getItemid($link['id'], 0);

                        if ($_itemid) {
                            $itemid = '&amp;Itemid=' . $_itemid;
                        }

                        $rows[$i]->href = $rows[$i]->href . $itemid;
                    }
                }
            }
            //	var_dump($rows);
            $total = $totalRows;
            $limit = intval(mosGetParam($_GET, 'limit', $mosConfig_list_limit));
            $limit = ($limit ? $limit : $mosConfig_list_limit);
            $limitstart = intval(mosGetParam($_GET, 'limitstart', 0));
            require_once ($GLOBALS['mosConfig_absolute_path'] .
                '/includes/pageNavigation.php');
            $pageNav = new mosPageNav($total, $limitstart, $limit);
            // prepares searchword for proper display in url
            $searchword_clean = urlencode($searchword_clean);
            unocloud_html::display($list, $params, $pageNav, $limitstart, $limit, $total, $totalRows,
                $searchword_clean);
        }
    }


}

$debug = intval(mosGetParam($_REQUEST, 'debug'));
$sid = intval(mosGetParam($_REQUEST, 'sid'));
$searchword = mosGetParam($_REQUEST, 'searchword');

if ((!empty($sid)) || (!empty($searchword))) {
    if (!empty($sid)) {
        $database->setQuery("SELECT * FROM #__unocloud_stat WHERE id='" . $sid . "'");
        $cur = $database->query();
        if ($row = mysql_fetch_object($cur)) {
            if (defined('_JEXEC')) {
                $_POST['searchword'] = $row->word;
            } else {
                $_REQUEST['searchword'] = $row->word;
            }
        }
    }

    if (defined('_JEXEC')) {
        $_REQUEST['task'] = "search";
        include ($mosConfig_absolute_path . "/components/com_search/search.php");
    } else {
        include ($mosConfig_absolute_path . "/components/com_search/search.html.php");
        include ($mosConfig_absolute_path . "/components/com_search/search.php");
    }
    //UNOCLOUD_search($search); //under construcion
} else {
    updateWords($debug);
}
?>